package xls;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DeltaPlusParser {

    public static final int TOUCH_TEL_ROW = 10068;
    public static final int TOUCH_KPK_ROW = 10897;
    public static final int GLASS_ROW = 11040;
    public static final int CAMERAS_ROW = 11110;
    public static final int BODY_PARTS_ROW = 12758;
    public static final int SOCKET_ROW = 12946;
    public static final int TRAIL_ROW = 13196;
    public static final int MICROPHONE_ROW = 12881;

    public static void main(String[] args) throws BiffException, IOException {
        Workbook workbook = Workbook.getWorkbook(new File("xls/delta-plus-price.xls"));
        Sheet sheet = workbook.getSheet(0);
        List<Part> parts = parseTouchTel(sheet, TOUCH_TEL_ROW);
        System.out.println(parts.size());
        parts = parseTouchTel(sheet, TOUCH_KPK_ROW);
        System.out.println(parts.size());
        parts = parseTouchTel(sheet, GLASS_ROW);
        System.out.println(parts.size());
        parts = parseTouchTel(sheet, CAMERAS_ROW);
        System.out.println(parts.size());
        parts = parseTouchTel(sheet, BODY_PARTS_ROW);
        System.out.println(parts.size());
        parts = parseTouchTel(sheet, SOCKET_ROW);
        System.out.println(parts.size());
        parts = parseTouchTel(sheet, TRAIL_ROW);
        System.out.println(parts.size());
        parts = parseTouchTel(sheet, MICROPHONE_ROW);
        System.out.println(parts.size());
    }

    public static List<Part> parseTouchTel(Sheet sheet, int num) {
        List<Part> parts = new ArrayList<Part>();
        while (true) {
            String name = sheet.getCell(0, num).getContents();
            String price = sheet.getCell(6, num).getContents().replace((char) 160, (char) 32).replaceAll("\\s", "");
            String count = sheet.getCell(8, num).getContents();
            num++;
            if (price.equals("")) {
                break;
            }
            Matcher matcher = Pattern.compile("\\S+\\/\\S+").matcher(name);
            if (matcher.find()) {
                String models[] = name.substring(matcher.start(), matcher.end()).split("/");
                for (String model : models) {
                    Part part = new Part();
                    part.price = Integer.parseInt(price);
                    part.count = count.equals("+") ? 100 : 99;
                    part.name = name.substring(0, matcher.start()) + model + name.substring(matcher.end(), name.length());
                    parts.add(part);
                }
            } else {
                Part part = new Part();
                part.price = Integer.parseInt(price);
                part.count = count.equals("+") ? 100 : 99;
                part.name = name;
                parts.add(part);
            }
        }
        return parts;
    }

}

class Part {
    String name;
    int price;
    int count;
}
